package com.nbclass.annotation;

import java.util.ArrayList;
import java.util.List;

/**
 * @标题 JavaMain
 * @作者 Mr.chen
 * @创建日期 2019/6/21 14:35
 * @版本号 1.0
 **/
public class JavaMain {

    public static void main(String[] args) {
//        List<String> list = mySplit("123456123214143134112333", "12");
//        System.out.println(list);


        String result = jianfa("111","1");
        System.out.println(result);
    }
    public static List<String> mySplit(String src,String target){
        // length   StringBuffer toCharArray
        List<String> list = new ArrayList<>();
        char[] srcArray = src.toCharArray();
        char[] targetArray = target.toCharArray();
        //  srcArray []  123456  targetArray []12
        StringBuffer sb = new StringBuffer("");
        for(int i=0;i<srcArray.length;i++){
            // StringBuffer
            if(srcArray[i]==targetArray[0]){
                //判断是否连续  已当前I 为下标
                boolean isEq = true;
                //3
                for(int j=0;j<targetArray.length;j++){
                    if(srcArray[i+j]!=targetArray[j]){
                        isEq=false;
                        break;
                    }
                }
                if(isEq){
                    String newStr = sb.toString();
                    if(!"".equals(newStr)){
                        list.add(newStr);
                    }
                    sb = new StringBuffer("");
                    i += targetArray.length -1 ;
                }else {
                    sb.append(srcArray[i]);
                }
            }else {
                //加入到一个字符串中
                sb.append(srcArray[i]);
            }
        }
        String lastStr = sb.toString();
        if(!"".equals(lastStr)){
            list.add(lastStr);
        }
        return list;
    }
    public static String jianfa(String str1,String str2){
        int[] a = new int[str1.length()];
        for(int i = 0;i<str1.length();i++){
            a[i] = Integer.parseInt(str1.substring(i,i+1));
        }
        int[] b = new int[str2.length()];
        for(int i = 0;i<str2.length();i++){
            b[i] = Integer.parseInt(str2.substring(i,i+1));
        }
            StringBuffer sb=new StringBuffer();
            boolean flag=false;//判断a是不是比b小
            if(a.length<b.length){
                int c[]=a;
                a=b;b=c;
                flag=true;
            }
            int a_len= a.length-1;
            int b_len=b.length-1;
            int jiewei=0;//借位
            while(a_len>=0||b_len>=0){
                int temp=0;
                if(a_len>=0&&b_len>=0){
                    if((a[a_len]-jiewei)<b[b_len]){
                        temp=a[a_len]+10-b[b_len]-jiewei;
                        jiewei=1;
                    }else{
                        temp=a[a_len]-b[b_len]-jiewei;
                    }
                }else if(a_len>=0){
                    temp=a[a_len]-jiewei;
                    jiewei=0;
                }
                sb.append(temp+"");
                a_len--;b_len--;
            }
            if(flag){
                sb.append("-").reverse();

            }else {
                sb.reverse();
            }
            while(sb.length() > 1 && sb.charAt(0) == '0') {
                sb.deleteCharAt(0);
            }
            return sb.toString();
    }
}
